package com.meego.common.misc;

import java.util.ArrayList;
import java.util.Iterator;

public class SafeIterator<T> implements Iterator<T> {

    private ArrayList<T> mList;
    private int mScan;

    public SafeIterator(Iterator<T> anIterator) {
        this.mList = new ArrayList<T>();

        while (anIterator.hasNext()) {
            this.mList.add(anIterator.next());
        }

        this.mScan = 0;
    }

    @Override
    public boolean hasNext() {
        return this.mScan < this.mList.size();
    }

    @Override
    public T next() {
        if (this.mScan >= this.mList.size()) {
            throw new IndexOutOfBoundsException("" + this.mScan);
        }
        return this.mList.get(this.mScan++);
    }

    @Override
    public void remove() {
        throw new Error("SafeIterator remove() not implemented");
    }
}